/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MongoDAL;

import Acquaintance.IACluster;
import DALFactory.IDataProcess;
import Entity.EClusterResult;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import java.util.*;

/**
 *
 * @author Administrator
 */
public class MongoDataProcess implements IDataProcess{
/**
 * 
 */
    public void removeAllCluster() {
        MongoStatement mstm=new MongoStatement();
        DBCollection colls=mstm.QueryTable("Cluster");
        colls.drop();
    }
/**
 * 
 * @param map 
 */
    public void PushClusterIntoDB(TreeMap map) {
      

      Iterator it = map.values().iterator();
      List<DBObject> listDB = new ArrayList<DBObject>();
     
        while (it.hasNext()) {
         IACluster cluster=(IACluster)it.next();
         DBObject obj=new BasicDBObject();
         obj.put("EValue", cluster.getEvalue());
         obj.put("Parent", cluster.getEparent());
         obj.put("Child1", cluster.getEchild1());
         obj.put("Child2", cluster.getEchild2());
         listDB.add(obj);
        }
      MongoStatement mstm=new MongoStatement();
      mstm.QueryTable("Cluster").insert(listDB);
    }
/**
 * 
 * @return 
 */
   public boolean ClusterIsEmty() {
        MongoStatement mstm=new MongoStatement();
        DBCollection colls=mstm.QueryTable("Cluster");
       if(colls.count()==0)
       {
           return true;
       }
       else
       {
          return false;
       }
    }
/**
 * 
 * @return 
 */
    public TreeMap<Integer, IACluster> getParentMap() {
        TreeMap<Integer, IACluster>  map=new TreeMap<Integer, IACluster> ();
       MongoStatement mstm=new MongoStatement();
        DBCollection colls=mstm.QueryTable("Cluster");
        DBCursor cur=colls.find();
        while(cur.hasNext())
        {
            DBObject obj=(DBObject)cur.next();
            IACluster cluster= mapCluster(obj);
            map.put(cluster.getEparent(), cluster);
        }
        return map;
    }
/**
 * 
 * @return 
 */
    public HashMap<Integer, IACluster> getChildenMap() {
         HashMap<Integer, IACluster>  map=new HashMap<Integer, IACluster> ();
       MongoStatement mstm=new MongoStatement();
        DBCollection colls=mstm.QueryTable("Cluster");
        DBCursor cur=colls.find();
        while(cur.hasNext())
        {
            DBObject obj=(DBObject)cur.next();
            IACluster cluster= mapCluster(obj);
            map.put(cluster.getEchild1(), cluster);
            map.put(cluster.getEchild2(), cluster);
        }
        return map;
    }
   
    /**
     * 
     * @param obj
     * @return 
     */
    private IACluster mapCluster(DBObject obj)
    {
        IACluster cluster=new EClusterResult();
        String Value=obj.get("EValue").toString();
        String Parent=obj.get("Parent").toString();
        String Child1=obj.get("Child1").toString();
        String Child2=obj.get("Child2").toString();
        float _value=Float.parseFloat(Value);
        float _parent=Float.parseFloat(Parent);
        float _child1=Float.parseFloat(Child1);
        float _child2=Float.parseFloat(Child2);      
        
        int value=(int)_value;
        int parent=(int)_parent;
        int child1=(int)_child1;
        int child2=(int)_child2;
        cluster.setEchild1(child1);
        cluster.setEchild2(child2);
        cluster.setEparent(parent);
        cluster.setEvalue(value);
        return cluster;
        
    }
    
}
